/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TOOL;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Endlesslove
 */
public  class Ulti 
{
    /**
     *
     * @param table
     * Xoa du lieu trong bang 
     */
    public static void CleanTabe(JTable table)
    {
        DefaultTableModel model = (DefaultTableModel)table.getModel(); 
        int rows = model.getRowCount(); 
        for(int i = rows - 1; i >=0; i--)
        {
           model.removeRow(i); 
        }
    }
    
    /**
     *
     * @param tablename : Ten bang
     * @param data du lieu dau vao
     * Dien du lieu tu resultset vao table
     */
    public static void FillData(JTable tablename, ResultSet data)
    {
        try {
            int rowCount = tablename.getColumnCount();
            int count = 1;
            while (data.next()) 
            {
                List<Object> objs = new ArrayList<>();
                objs.add(count ++);

                for(int i = 1; i < rowCount ; i++)
                {
                    try
                    {
                        objs.add(data.getObject(i));
                    }
                    catch(Exception ie)
                    {

                    }
                }
                ((DefaultTableModel)tablename.getModel()).addRow(objs.toArray()
                );
            }
        } catch (SQLException ex) {
            Logger.getLogger(Ulti.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void FillCobobox(JComboBox combo, ResultSet res, int index)
    {
        try 
        {
            while (res.next())
            {
                combo.addItem(res.getObject(index));
            }
        } 
        catch (SQLException ex)
        {
            Logger.getLogger(Ulti.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
   
    /**
     *
     * @param jtext
     * @return Gia trij trong textfeld
     */
    public static String S(JTextField jtext)
    {
        return jtext.getText().trim();
    }
}
